package com.example.gulimall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.vo.BaseAttrs;
import com.example.gulimall.product.vo.item.SpuBaseAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.product.dao.ProductAttrValueDao;
import com.example.gulimall.product.entity.ProductAttrValueEntity;
import com.example.gulimall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBaseAttrs(List<BaseAttrs> baseAttrs, Long spuInfoId) {

        if (CollectionUtils.isEmpty(baseAttrs)) {
            return ;
        }

        List<ProductAttrValueEntity> entities = baseAttrs.stream().map(
                a -> new ProductAttrValueEntity()
                        .setSpuId(spuInfoId)
                        .setAttrId(a.getAttrId())
                        .setQuickShow(a.getShowDesc())
                        .setAttrValue(a.getAttrValues())
                        .setAttrName(this.attrService.getById(a.getAttrId()).getAttrName())
        ).collect(Collectors.toList());

        this.saveBatch(entities);
    }

    @Override
    public List<ProductAttrValueEntity> listForSpu(Long spuId) {

        return this.baseMapper.selectList(
                Wrappers.<ProductAttrValueEntity>lambdaQuery()
                .eq(ProductAttrValueEntity::getSpuId, spuId)
        );
    }

    @Override
    public void updateSpuAttr(List<ProductAttrValueEntity> values, Long spuId) {

        // 删除原始数据
        this.baseMapper.delete(
                Wrappers.<ProductAttrValueEntity>lambdaQuery()
                .eq(ProductAttrValueEntity::getSpuId, spuId)
        );

        values = values.stream().map(
                a -> a.setSpuId(spuId)
        ).collect(Collectors.toList());

        this.saveBatch(values);
    }

    @Override
    public List<SpuBaseAttrVo> selectBatchByAttrIds(Set<Long> attrIds) {
        if (CollectionUtil.isEmpty(attrIds)) {
            return Collections.emptyList();
        }
        return this.baseMapper.selectBatchByAttrIds(attrIds);
    }

    @Override
    public List<String> getSaleAttr(Long skuId) {
        if (Objects.nonNull(skuId)) {
            return Collections.emptyList();
        }
        List<String> attrs = this.baseMapper.selectSaleAttr (skuId);
        return CollectionUtil.isNotEmpty(attrs) ? attrs : Collections.emptyList();
    }
}